package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.model.Coordinate;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.StoreService;
import com.youi.entity.Store;
import com.youi.entity.User;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JedisUtils;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 门店信息控制层
* @author Administrator
*/
@Controller
public class StoreController  extends HttpRequestManage{

		@Autowired
		private StoreService storeService;

		/**
		 *添加或编辑门店信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditStore")
		public String addOrEditStore(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Store store=null;
			if (id!=null) {
				store=storeService.get(id);
			}else {
				store=new Store();
				store.setDelStatus(DelStatus.NODEL);
				store.setGmtCreated(new Date());
			}
			String name=optString("name",request);
			if (name!=null){
				store.setName(name);
			}
			String code=optString("code",request);
			if (code!=null){
				store.setCode(code);
			}
			String phone=optString("phone",request);
			if (phone!=null){
				store.setPhone(phone);
			}
			String province=optString("province",request);
			if (province!=null){
				store.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				store.setCity(city);
			}
			String district=optString("district",request);
			if (district!=null){
				store.setDistrict(district);
			}
			String address=optString("address",request);
			if (address!=null){
				store.setAddress(address);
			}
			String wgs84Lon=optString("wgs84Lon",request);
			if (wgs84Lon!=null){
				store.setWgs84Lon(wgs84Lon);
			}
			String wgs84Lat=optString("wgs84Lat",request);
			if (wgs84Lat!=null){
				store.setWgs84Lat(wgs84Lat);
			}
			String amapLon=optString("amapLon",request);
			if (amapLon!=null){
				store.setAmapLon(amapLon);
			}
			String amapLat=optString("amapLat",request);
			if (amapLat!=null){
				store.setAmapLat(amapLat);
			}
			String tenxunLon=optString("tenxunLon",request);
			if (tenxunLon!=null){
				store.setTenxunLon(tenxunLon);
			}
			String tengxunLat=optString("tengxunLat",request);
			if (tengxunLat!=null){
				store.setTengxunLat(tengxunLat);
			}
			String baiduLon=optString("baiduLon",request);
			if (baiduLon!=null){
				store.setBaiduLon(baiduLon);
			}
			String baiduLat=optString("baiduLat",request);
			if (baiduLat!=null){
				store.setBaiduLat(baiduLat);
			}
			store.setGmtModify(new Date());
			storeService.saveOrUpdate(store);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除门店信息
		*/
		@ResponseBody
		@RequestMapping("delStoreByIds")
		public String delStoreByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Store> storeList= storeService.getByParams(params);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Store store : storeList) {
				store.setDelStatus(DelStatus.DEL);
			}
			storeService.saveOrUpdateAll(storeList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询门店信息
		*/
		@ResponseBody
		@RequestMapping("getStore")
		public String getStore(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Store> storeList=storeService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=storeService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, storeList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询门店信息
		*/
		@ResponseBody
		@RequestMapping("getStoreById")
		public String getStoreById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Store store=storeService.get(id);
			if (store==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",store);//jsonObject	json对象
		}

		/**
		 * M-38
		 * 查询关联门店
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/getLeaderStore")
		public String getAllStore(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put("mobile", user.getPhone());
			List<Store> storeList= storeService.getByParams(params);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "手机号无可关联认证的门店");
			}
			storeService.getAuthStoreStatus(storeList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",storeList);//jsonObjectList	json对象集合
		}
		
		private static String  DISTANCE="distance";//距离
		
		/**
		 * M-18
		 * 查询门店列表（按距离近到远排序）
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/getStoreByDistance")
		public String getStoreByDistance(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			//当前位置的经纬度
			Double longitude=getDouble("longitude", request);
			Double latitude=getDouble("latitude", request);
			if (pageIndex==null) {
				pageIndex=1;
			}
			if (pageNum==null) {
				pageNum=10;
			}
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(DISTANCE,Common.ASC);
			Map<String, Object> searchPram=null;
			if (pms!=null&&pms.length()>0) {
				searchPram=new HashMap<String, Object>();
				searchPram.put("name", pms);
				searchPram.put("code", pms);
				searchPram.put("address", pms);
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Store> storeList= storeService.getNearbyStoreList(prams, searchPram, sortPram, (pageIndex-1)*pageNum, pageNum, startTime, endTime, latitude, longitude);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=storeService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, storeList);	
			storeService.getAuthStoreStatus(storeList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",storeList);//jsonObjectList	json对象集合
		}
}
